package com.hecore.lagou.speak.component;

import com.hecore.lagou.speak.entity.SpeakBean;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @Author: hecore
 * @Date: 2020/10/18 19:59
 * @Description: 重写reducer
 * 我们设置的key就是deviceId，所以从key中取的就是
 */

public class SpeakReducer extends Reducer<Text, SpeakBean, Text, SpeakBean> {

    @Override
    protected void reduce(Text key, Iterable<SpeakBean> values, Context context) throws IOException, InterruptedException  {
        // 定义初始值
        long self_Duration = 0;
        long thirdPart_Duration = 0;
        // 1 遍历所⽤bean，将其中的⾃有，第三⽅时⻓分别累加
        for (SpeakBean sb : values) {
            self_Duration += sb.getSelfDuration();
            thirdPart_Duration += sb.getThirdPartDuration();
        }
        // 2 封装对象
        SpeakBean resultBean = new SpeakBean(self_Duration,
                thirdPart_Duration,key.toString());
        // 3 写出
        context.write(key, resultBean);
    }

}

